package antel.com.uy;

import java.net.*;
import java.io.*;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.xml.sax.SAXException;

import antel.com.constant.Constants;



public class AgenciaMultiServer {
	private static errors logSys = errors.getInstance();
	
	  public static void main(String[] args) throws IOException {
	        ServerSocket serverSocket = null;
	        boolean listening = true;
	        ConfigParam configParam = ConfigParam.getInstance();
	        
			try {

				configParam.cargarParametros();
			    serverSocket = new ServerSocket(Integer.parseInt(Constants.getGlobalParam().get("port")));	
			    logSys.writeError(Constants.getLoginok() + " Port:" + Constants.getGlobalParam().get("port"));
			    System.out.println(Constants.getLoginok() + " Port:" + Constants.getGlobalParam().get("port"));
			
			} catch (ParserConfigurationException e1) {
				
				logSys.writeError(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e1.getMessage());
				System.out.println(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e1.getMessage());
			    System.exit(-1);
		        
			} catch (SAXException e1) {
				
				logSys.writeError(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e1.getMessage());
				System.out.println(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e1.getMessage());
			    System.exit(-1);
		        
			} catch (IOException e) {
	        	logSys.writeError(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e.getMessage());
	        	System.out.println(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e.getMessage());
	            System.exit(-1);
	        
			} catch (TransformerFactoryConfigurationError e) {
	        	logSys.writeError(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e.getMessage());
	        	System.out.println(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e.getMessage());
	            System.exit(-1);
		        
	        } catch (TransformerException e) {
	        	logSys.writeError(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e.getMessage());
	        	System.out.println(Constants.getLoginerror() + " Port:" +  Constants.getGlobalParam().get("port")+" "+e.getMessage());
	            System.exit(-1);
		        
	        } catch (CopyFileConfigAlert e) {
	        	logSys.writeError(Constants.getLoginerror() + " "+e.getMessage());
	        	System.out.println(Constants.getLoginerror() + " "+e.getMessage());
	            System.exit(-1);
		        
	        }
	 
	        while (listening)
	        new AgenciaMultiServerThread(serverSocket.accept()).start();
	 
	        serverSocket.close();
	    }
}
